#include "Answer4_Demo.h"
#include "unistd.h"

//拓展：

/*
    高级模式下，可实现可调的位数和可调的文件个数，
    其中，启用5个线程来进行文件读入，每个线程都采用不同的同步方式。
*/

static VOID caculatePi(int num1,int num2,char *buf){
    long *pi, *t, m, n, r, s;
    int t0[][3] = {48, 32, 20, 24, 8, 4}, k0[][3] = {1, 1, 0, 1, 1, 1};
    int n0[][3] = {18, 57, 239, 8, 57, 239}, d, i, j, k, p, q;
    // d = (argc > 1) ? (((i = atoi(argv[1])) < 0) ? 0 : i) : 1000;
    // q = (argc > 2) ? 1 : 0;
    d = 1000;
    q = 0;
    // printf("%s\n\n", "Nature (R) Pi value compute Program  (C) Tue 1999.11.30");
    // printf("pi= %s%d * arctg(1/%d) %s %d * arctg(1/%d) %s %d * arctg(1/%d) [%s]\n",
    //        k0[q][0] ? "" : "-", t0[q][0], n0[q][0], k0[q][1] ? "+" : "-", t0[q][1],
    //        n0[q][1], k0[q][2] ? "+" : "-", t0[q][2], n0[q][2], q ? "Stomer" : "Gauss");
    if ((t = (long *) calloc((d += 5) + 1, sizeof(long))) == NULL) return 1;
    if ((pi = (long *) calloc(d + 1, sizeof(long))) == NULL) return 2;
    for (i = d; i >= 0; i--) pi[i] = 0;
    for (p = 0; p < 3; p++) {
        for (k = k0[q][p], n = n0[q][p], t[i = j = d] = t0[q][p], i--; i >= 0; i--) t[i] = 0;
        for (r = 0, i = j; i >= 0; i--) {
            r = (m = 10 * r + t[i]) % n;
            t[i] = m / n;
            k ? (pi[i] += t[i]) : (pi[i] -= t[i]);
        }
        while (j > 0 && t[j] == 0) j--;
        for (k = !k, s = 3, n *= n; j > 0; k = !k, s += 2) {
            for (r = 0, i = j; i >= 0; i--) {
                r = (m = 10 * r + t[i]) % n;
                t[i] = m / n;
            }
            while (j > 0 && t[j] == 0) j--;
            for (r = 0, i = j; i >= 0; i--) {
                r = (m = 10 * r + t[i]) % s;
                m /= s;
                k ? (pi[i] += m) : (pi[i] -= m);
            }
        }
    }
    for (n = i = 0; i <= d; pi[i++] = r) {
        n = (m = pi[i] + n) / 10;
        if ((r = m % 10) < 0) r += 10, n--;
    }
    // printf("pi= %ld.", pi[d]);
    // for (i = d - 1; i >= 5; i--)
    //     printf("%ld%s", pi[i], ((m = d - i + 5) % 65) ? ((m % 5) ? "" : " ") : "\n");
    // printf("%sDIGITS: %d\n", (m % 65) ? "\n" : "", d - 5);

    char temp[2000];
    for(int x = d;x>=5;x--){
        temp[x] = pi[x]+'0';
    }

    int index = 0;
    //位数截取
    for(int x = d - num1;x >= 5;x--){
        buf[index++] = temp[x];
        if(index==num2-num1+1){
            buf[index] = '\0';
            break;
        }
    }

    return;
}

static VOID Task01(){
    UINT32 ret;
    if(g_syncMode==1){
        LOS_MuxCreate(&g_AnswerMutex);

        LOS_MuxPend(&g_AnswerMutex,LOS_WAIT_FOREVER);

        printf("\nThis is Task1\n");
        if(g_saveMode==1){
            int fd = open("/ram/pi1_250.txt", O_RDWR | O_CREAT, 0777);
            char buf[500];
            (VOID)memset_s(buf, sizeof(buf), 0, 500);
            caculatePi(1,250,buf);
            write(fd,"\nA:",3);
            int size = write(fd,buf,251);
            if(size<0){
                printf("\n------------write error-----------\n");
            }
            close(fd);
        }
        if(g_saveMode==2){
            g_fd = open("/ram/pi1000.txt", O_RDWR | O_CREAT, 0777);
            char buf[500];
            (VOID)memset_s(buf, sizeof(buf), 0, 500);
            caculatePi(1,250,buf);
            write(g_fd,"\nA:",3);
            write(g_fd,buf,250);
        }

        LOS_MuxPost(&g_AnswerMutex);
    }
    else if(g_syncMode==2){
        ret = LOS_SemCreate(SEM_COUNT,&g_AnswerSem);

        LOS_SemPend(&g_AnswerSem,LOS_WAIT_FOREVER);

        printf("\nThis is Task1\n");
        if(g_saveMode==1){
            int fd = open("/ram/pi1_250.txt", O_RDWR | O_CREAT, 0777);
            char buf[500];
            (VOID)memset_s(buf, sizeof(buf), 0, 500);
            caculatePi(1,250,buf);
            write(fd,"\nA:",3);
            write(fd,buf,251);
            close(fd);
        }
        if(g_saveMode==2){
            g_fd = open("/ram/pi1000.txt", O_RDWR | O_CREAT, 0777);
            char buf[500];
            (VOID)memset_s(buf, sizeof(buf), 0, 500);
            caculatePi(1,250,buf);
            write(g_fd,"\nA:",3);
            write(g_fd,buf,250);
        }

        LOS_SemPost(&g_AnswerMutex);
    }
    else if(g_syncMode==3){
        ret = LOS_EventInit(&g_AnswerEvent);

        LOS_EventWrite(&g_AnswerEvent,0x001);

        printf("\nThis is Task1\n");
        if(g_saveMode==1){
            int fd = open("/ram/pi1_250.txt", O_RDWR | O_CREAT, 0777);
            char buf[500];
            (VOID)memset_s(buf, sizeof(buf), 0, 500);
            caculatePi(1,250,buf);
            write(fd,"\nA:",3);
            write(fd,buf,251);
            close(fd);
        }
        if(g_saveMode==2){
            g_fd = open("/ram/pi1000.txt", O_RDWR | O_CREAT, 0777);
            char buf[500];
            (VOID)memset_s(buf, sizeof(buf), 0, 500);
            caculatePi(1,250,buf);
            write(g_fd,"\nA:",3);
            write(g_fd,buf,250);
        }
    }
    else if(g_syncMode==4){
        ret = LOS_QueueCreate("Queue1",1,&g_AnswerQueue,0,QUEUE_SHORT_BUFFER_LENGTH);

        printf("\nThis is Task1\n");
        if(g_saveMode==1){
            int fd = open("/ram/pi1_250.txt", O_RDWR | O_CREAT, 0777);
            char buf[500];
            (VOID)memset_s(buf, sizeof(buf), 0, 500);
            caculatePi(1,250,buf);
            write(fd,"\nA:",3);
            write(fd,buf,251);
            close(fd);
        }
        if(g_saveMode==2){
            g_fd = open("/ram/pi1000.txt", O_RDWR | O_CREAT, 0777);
            char buf[500];
            (VOID)memset_s(buf, sizeof(buf), 0, 500);
            caculatePi(1,250,buf);
            write(g_fd,"\nA:",3);
            write(g_fd,buf,250);
        }

        LOS_QueueWriteCopy(g_AnswerQueue, &sendBuffer1 , QUEUE_SHORT_BUFFER_LENGTH, LOS_WAIT_FOREVER);
    }
}

static VOID Task02(){
    UINT32 ret;
    if(g_syncMode==1){
        LOS_MuxPend(&g_AnswerMutex,LOS_WAIT_FOREVER);

        printf("\nThis is Task2\n");
        if(g_saveMode==1){
            int fd = open("/ram/pi251_500.txt", O_RDWR | O_CREAT, 0777);
            char buf[500];
            (VOID)memset_s(buf, sizeof(buf), 0, 500);
            caculatePi(251,500,buf);
            write(fd,"\nB:",3);
            write(fd,buf,251);
            close(fd);
        }
        if(g_saveMode==2){
            char buf[500];
            (VOID)memset_s(buf, sizeof(buf), 0, 500);
            caculatePi(251,500,buf);
            write(g_fd,"\nB:",3);
            write(g_fd,buf,250);
        }


        LOS_MuxPost(&g_AnswerMutex);
    }
    else if(g_syncMode==2){
        LOS_SemPend(&g_AnswerSem,LOS_WAIT_FOREVER);

        printf("\nThis is Task2\n");
        if(g_saveMode==1){
            int fd = open("/ram/pi251_500.txt", O_RDWR | O_CREAT, 0777);
            char buf[500];
            (VOID)memset_s(buf, sizeof(buf), 0, 500);
            caculatePi(251,500,buf);
            write(fd,"\nB:",3);
            write(fd,buf,251);
            close(fd);
        }
        if(g_saveMode==2){
            char buf[500];
            (VOID)memset_s(buf, sizeof(buf), 0, 500);
            caculatePi(251,500,buf);
            write(g_fd,"\nB:",3);
            write(g_fd,buf,250);
        }

        LOS_SemPost(&g_AnswerMutex);
    }
    else if(g_syncMode==3){
        LOS_EventRead(&g_AnswerEvent,0x001,LOS_WAITMODE_AND,LOS_WAIT_FOREVER);

        printf("\nThis is Task2\n");
        if(g_saveMode==1){
            int fd = open("/ram/pi251_500.txt", O_RDWR | O_CREAT, 0777);
            char buf[500];
            (VOID)memset_s(buf, sizeof(buf), 0, 500);
            caculatePi(251,500,buf);
            write(fd,"\nB:",3);
            write(fd,buf,251);
            close(fd);
        }
        if(g_saveMode==2){
            char buf[500];
            (VOID)memset_s(buf, sizeof(buf), 0, 500);
            caculatePi(251,500,buf);
            write(g_fd,"\nB:",3);
            write(g_fd,buf,250);
        }

        LOS_EventWrite(&g_AnswerEvent,0x010);
    }
    else if(g_syncMode==4){
        LOS_QueueReadCopy(g_AnswerQueue, &recvBuffer,QUEUE_SHORT_BUFFER_LENGTH,LOS_WAIT_FOREVER);

        if(recvBuffer==NULL){
            printf("\nNULL strp!\n");
            return;
        }

        printf("\nTASK2 receive message = %s\n",recvBuffer);

        printf("\nThis is Task2\n");
        if(g_saveMode==1){
            int fd = open("/ram/pi251_500.txt", O_RDWR | O_CREAT, 0777);
            char buf[500];
            (VOID)memset_s(buf, sizeof(buf), 0, 500);
            caculatePi(251,500,buf);
            write(fd,"\nB:",3);
            write(fd,buf,251);
            close(fd);
        }
        if(g_saveMode==2){
            char buf[500];
            (VOID)memset_s(buf, sizeof(buf), 0, 500);
            caculatePi(251,500,buf);
            write(g_fd,"\nB:",3);
            write(g_fd,buf,250);
        }

        LOS_QueueWriteCopy(g_AnswerQueue, &sendBuffer2,QUEUE_SHORT_BUFFER_LENGTH,LOS_WAIT_FOREVER);
    }
}

static VOID Task03(){
    UINT32 ret;
    if(g_syncMode==1){
        LOS_MuxPend(&g_AnswerMutex,LOS_WAIT_FOREVER);

        printf("\nThis is Task3\n");
        if(g_saveMode==1){
            int fd = open("/ram/pi501_750.txt", O_RDWR | O_CREAT, 0777);
            char buf[500];
            (VOID)memset_s(buf, sizeof(buf), 0, 500);
            caculatePi(501,750,buf);
            write(fd,"\nC:",3);
            write(fd,buf,250);
            close(fd);
        }
        if(g_saveMode==2){
            char buf[500];
            (VOID)memset_s(buf, sizeof(buf), 0, 500);
            caculatePi(501,750,buf);
            write(g_fd,"\nC:",3);
            write(g_fd,buf,250);
        }

        LOS_MuxPost(&g_AnswerMutex);
    }
    else if(g_syncMode==2){
        LOS_SemPend(&g_AnswerSem,LOS_WAIT_FOREVER);

        printf("\nThis is Task3\n");
        if(g_saveMode==1){
            int fd = open("/ram/pi501_750.txt", O_RDWR | O_CREAT, 0777);
            char buf[500];
            (VOID)memset_s(buf, sizeof(buf), 0, 500);
            caculatePi(501,750,buf);
            write(fd,"\nC:",3);
            write(fd,buf,250);
            close(fd);
        }
        if(g_saveMode==2){
            char buf[500];
            (VOID)memset_s(buf, sizeof(buf), 0, 500);
            caculatePi(501,750,buf);
            write(g_fd,"\nC:",3);
            write(g_fd,buf,250);
        }

        LOS_SemPost(&g_AnswerMutex);
    }
    else if(g_syncMode==3){
        LOS_EventRead(&g_AnswerEvent,0x011,LOS_WAITMODE_AND,LOS_WAIT_FOREVER);

        printf("\nThis is Task3\n");
        if(g_saveMode==1){
            int fd = open("/ram/pi501_750.txt", O_RDWR | O_CREAT, 0777);
            char buf[500];
            (VOID)memset_s(buf, sizeof(buf), 0, 500);
            caculatePi(501,750,buf);
            write(fd,"\nC:",3);
            write(fd,buf,250);
            close(fd);
        }
        if(g_saveMode==2){
            char buf[500];
            (VOID)memset_s(buf, sizeof(buf), 0, 500);
            caculatePi(501,750,buf);
            write(g_fd,"\nC:",3);
            write(g_fd,buf,250);
        }

        LOS_EventWrite(&g_AnswerEvent,0x100);
    }
    else if(g_syncMode==4){
        LOS_QueueReadCopy(g_AnswerQueue, &recvBuffer,QUEUE_SHORT_BUFFER_LENGTH,LOS_WAIT_FOREVER);

        if(recvBuffer==NULL){
            printf("\nNULL strp!\n");
            return;
        }

        printf("\nTASK3 receive message = %s\n",recvBuffer);

        printf("\nThis is Task3\n");
        if(g_saveMode==1){
            int fd = open("/ram/pi501_750.txt", O_RDWR | O_CREAT, 0777);
            char buf[500];
            (VOID)memset_s(buf, sizeof(buf), 0, 500);
            caculatePi(501,750,buf);
            write(fd,"\nC:",3);
            write(fd,buf,250);
            close(fd);
        }
        if(g_saveMode==2){
            char buf[500];
            (VOID)memset_s(buf, sizeof(buf), 0, 500);
            caculatePi(501,750,buf);
            write(g_fd,"\nC:",3);
            write(g_fd,buf,250);
        }

        LOS_QueueWriteCopy(g_AnswerQueue, &sendBuffer3,QUEUE_SHORT_BUFFER_LENGTH,LOS_WAIT_FOREVER);
    }
}

static VOID Task04(){
    UINT32 ret;
    if(g_syncMode==1){
        LOS_MuxPend(&g_AnswerMutex,LOS_WAIT_FOREVER);

        printf("\nThis is Task4\n");
        if(g_saveMode==1){
            int fd = open("/ram/pi751_1000.txt", O_RDWR | O_CREAT, 0777);
            char buf[500];
            (VOID)memset_s(buf, sizeof(buf), 0, 500);
            caculatePi(751,1000,buf);
            write(fd,"\nD:",3);
            write(fd,buf,250);
            close(fd);
        }
        if(g_saveMode==2){
            char buf[500];
            (VOID)memset_s(buf, sizeof(buf), 0, 500);
            caculatePi(751,1000,buf);
            write(g_fd,"\nD:",3);
            write(g_fd,buf,251);
            close(g_fd);
        }

        LOS_MuxPost(&g_AnswerMutex);
    }
    else if(g_syncMode==2){
        LOS_SemPend(&g_AnswerSem,LOS_WAIT_FOREVER);

        printf("\nThis is Task4\n");
        if(g_saveMode==1){
            int fd = open("/ram/pi751_1000.txt", O_RDWR | O_CREAT, 0777);
            char buf[500];
            (VOID)memset_s(buf, sizeof(buf), 0, 500);
            caculatePi(751,1000,buf);
            write(fd,"\nD:",3);
            write(fd,buf,250);
            close(fd);
        }
        if(g_saveMode==2){
            char buf[500];
            (VOID)memset_s(buf, sizeof(buf), 0, 500);
            caculatePi(751,1000,buf);
            write(g_fd,"\nD:",3);
            write(g_fd,buf,251);
            close(g_fd);
        }

        LOS_SemPost(&g_AnswerMutex);
    }
    else if(g_syncMode==3){
        LOS_EventRead(&g_AnswerEvent,0x111,LOS_WAITMODE_AND,LOS_WAIT_FOREVER);

        printf("\nThis is Task4\n");
        if(g_saveMode==1){
            int fd = open("/ram/pi751_1000.txt", O_RDWR | O_CREAT, 0777);
            char buf[500];
            (VOID)memset_s(buf, sizeof(buf), 0, 500);
            caculatePi(751,1000,buf);
            write(fd,"\nD:",3);
            write(fd,buf,250);
            close(fd);
        }
        if(g_saveMode==2){
            char buf[500];
            (VOID)memset_s(buf, sizeof(buf), 0, 500);
            caculatePi(751,1000,buf);
            write(g_fd,"\nD:",3);
            write(g_fd,buf,251);
            close(g_fd);
        }
    }
    else if(g_syncMode==4){
        LOS_QueueReadCopy(g_AnswerQueue, &recvBuffer,QUEUE_SHORT_BUFFER_LENGTH,LOS_WAIT_FOREVER);
        
        if(recvBuffer==NULL){
            printf("\nNULL strp!\n");
            return;
        }
        
        printf("\nTASK4 receive message = %s\n",recvBuffer);

        printf("\nThis is Task4\n");
        if(g_saveMode==1){
            int fd = open("/ram/pi751_1000.txt", O_RDWR | O_CREAT, 0777);
            char buf[500];
            (VOID)memset_s(buf, sizeof(buf), 0, 500);
            caculatePi(751,1000,buf);
            write(fd,"\nD:",3);
            write(fd,buf,250);
            close(fd);
        }
        if(g_saveMode==2){
            char buf[500];
            (VOID)memset_s(buf, sizeof(buf), 0, 500);
            caculatePi(751,1000,buf);
            write(g_fd,"\nD:",3);
            write(g_fd,buf,251);
            close(g_fd);
        }
    }
}

static VOID HyperTask01(){
    UINT32 ret;
    //使用互斥锁
    LOS_MuxPend(&g_AnswerMutexForHyperMode,LOS_WAIT_FOREVER);

    printf("\nThis is HyperTask1\n");

    digitTemp = 1;
    fileNumTemp = 0;
    int ave = digit/fileNum;

    cnt = fileNum/5;
    for(int i = 1;i<=cnt;i++){
        int fd = open(fileName1, O_RDWR | O_CREAT, 0777);
        char buf[1000];
        (VOID)memset_s(buf, sizeof(buf), 0, 1000);
        caculatePi(digitTemp,digitTemp+ave-1,buf);
        write(fd,buf,ave);
        close(fd);
        digitTemp = digitTemp+ave+1;
        fileNumTemp++;
        fileName1[7] = '0'+ fileNumTemp/100;
        fileName1[8] = '0'+ (fileNumTemp/10)%10;
        fileName1[9] = '0'+ fileNumTemp%10;
    }

    LOS_TaskDelay(200);

    printf("\nTask1 over\n");

    LOS_MuxPost(&g_AnswerMutexForHyperMode);
}

static VOID HyperTask02(){
    UINT32 ret;
    LOS_MuxPend(&g_AnswerMutexForHyperMode,LOS_WAIT_FOREVER);
    printf("\nPend Mutex Success\n");
    LOS_MuxPost(&g_AnswerMutexForHyperMode);
    //使用信号量
    LOS_SemCreate(SEM_COUNT,&g_AnswerSemForHyperMode);

    LOS_SemPend(&g_AnswerSemForHyperMode,LOS_WAIT_FOREVER);

    printf("\nThis is HyperTask2\n");
    
    fileName2[7] = '0'+ cnt/100;
    fileName2[8] = '0'+ (cnt/10)%10;
    fileName2[9] = '0'+ cnt%10;

    int ave = digit/fileNum;
    for(int i = cnt+1;i<=2*cnt;i++){
        int fd = open(fileName2, O_RDWR | O_CREAT, 0777);
        char buf[1000];
        (VOID)memset_s(buf, sizeof(buf), 0, 1000);
        caculatePi(digitTemp,digitTemp+ave-1,buf);
        write(fd,buf,ave);
        close(fd);
        digitTemp = digitTemp+ave+1;
        fileNumTemp++;
        fileName2[7] = '0'+ fileNumTemp/100;
        fileName2[8] = '0'+ (fileNumTemp/10)%10;
        fileName2[9] = '0'+ fileNumTemp%10;
    }

    LOS_TaskDelay(200);

    LOS_SemPost(&g_AnswerSemForHyperMode);
}

static VOID HyperTask03(){
    UINT32 ret;
    LOS_SemPend(&g_AnswerSemForHyperMode,LOS_WAIT_FOREVER);
    printf("\nPend Sem Success\n");
    LOS_SemPost(&g_AnswerSemForHyperMode);
    //使用事件
    ret = LOS_EventInit(&g_AnswerEventForHyperMode);

    printf("\nThis is HyperTask3\n");

    fileName3[7] = '0'+ (2*cnt)/100;
    fileName3[8] = '0'+ ((2*cnt)/10)%10;
    fileName3[9] = '0'+ (2*cnt)%10;

    int ave = digit/fileNum;
    for(int i = 2*cnt+1;i<=3*cnt;i++){
        int fd = open(fileName3, O_RDWR | O_CREAT, 0777);
        char buf[1000];
        (VOID)memset_s(buf, sizeof(buf), 0, 1000);
        caculatePi(digitTemp,digitTemp+ave-1,buf);
        write(fd,buf,ave);
        close(fd);
        digitTemp = digitTemp+ave+1;
        fileNumTemp++;
        fileName3[7] = '0'+ fileNumTemp/100;
        fileName3[8] = '0'+ (fileNumTemp/10)%10;
        fileName3[9] = '0'+ fileNumTemp%10;
    }

    LOS_TaskDelay(200);

    LOS_EventWrite(&g_AnswerEventForHyperMode,0x1);

}

static VOID HyperTask04(){
    UINT32 ret;
    LOS_EventRead(&g_AnswerEventForHyperMode,0x1,LOS_WAITMODE_OR,LOS_WAIT_FOREVER);
    printf("\nRead Event Success\n");

    ret = LOS_QueueCreate("HyperQueue",1,&g_AnswerQueueForHyperMode,0,QUEUE_SHORT_BUFFER_LENGTH);
    printf("\nThis is HyperTask4\n");

    fileName4[7] = '0'+ (3*cnt)/100;
    fileName4[8] = '0'+ ((3*cnt)/10)%10;
    fileName4[9] = '0'+ (3*cnt)%10;

    int ave = digit/fileNum;
    for(int i = 3*cnt+1;i<=4*cnt;i++){
        int fd = open(fileName4, O_RDWR | O_CREAT, 0777);
        char buf[1000];
        (VOID)memset_s(buf, sizeof(buf), 0, 1000);
        caculatePi(digitTemp,digitTemp+ave-1,buf);
        write(fd,buf,ave);
        close(fd);
        digitTemp = digitTemp+ave+1;
        fileNumTemp++;
        fileName4[7] = '0'+ fileNumTemp/100;
        fileName4[8] = '0'+ (fileNumTemp/10)%10;
        fileName4[9] = '0'+ fileNumTemp%10;
    }

    LOS_TaskDelay(200);

    LOS_QueueWriteCopy(g_AnswerQueueForHyperMode, &sendBufferForHyperMode , QUEUE_SHORT_BUFFER_LENGTH, LOS_WAIT_FOREVER);
    printf("\nsend message = %s\n",sendBufferForHyperMode);
}

static VOID HyperTask05(){
    LOS_TaskDelay(1000);//让task4先发消息

    UINT32 ret;
    printf("\nenter HyperTask 5!\n");
    LOS_QueueReadCopy(g_AnswerQueueForHyperMode, &recvBufferForHyperMode,QUEUE_SHORT_BUFFER_LENGTH,LOS_WAIT_FOREVER);

    if(recvBufferForHyperMode==NULL){
        printf("\nNULL strp!\n");
        return;
    }

    printf("\nHyperTask5 receive message = %s\n",recvBufferForHyperMode);
    printf("\nThis is HyperTask5!\n");

    fileName5[7] = '0'+ (4*cnt)/100;
    fileName5[8] = '0'+ ((4*cnt)/10)%10;
    fileName5[9] = '0'+ (4*cnt)%10;

    int ave = digit/fileNum;
    for(int i = 4*cnt+1;i<=5*cnt;i++){
        int fd = open(fileName5, O_RDWR | O_CREAT, 0777);
        char buf[1000];
        (VOID)memset_s(buf, sizeof(buf), 0, 1000);
        caculatePi(digitTemp,digitTemp+ave-1,buf);
        write(fd,buf,ave);
        close(fd);
        digitTemp = digitTemp+ave+1;
        fileNumTemp++;
        fileName5[7] = '0'+ fileNumTemp/100;
        fileName5[8] = '0'+ (fileNumTemp/10)%10;
        fileName5[9] = '0'+ fileNumTemp%10;
    }

    LOS_TaskDelay(200);

}

static UINT32 Create_Task01(){
    UINT32 ret;
    TSK_INIT_PARAM_S task1 = { 0 };
    task1.pfnTaskEntry = (TSK_ENTRY_FUNC)Task01;
    task1.uwStackSize = TASK_STACK_SIZE_TEST*10;
    task1.pcName = "Task1-250";
    task1.usTaskPrio = TASK_PRIO_TEST - 1;
    task1.uwResved = LOS_TASK_STATUS_DETACHED;

    ret = LOS_TaskCreate(&g_AnswerTaskID01,&task1);

    return LOS_OK;
}

static UINT32 Create_Task02(){
    UINT32 ret;
    TSK_INIT_PARAM_S task2 = { 0 };
    task2.pfnTaskEntry = (TSK_ENTRY_FUNC)Task02;
    task2.uwStackSize = TASK_STACK_SIZE_TEST*10;
    task2.pcName = "Task251-500";
    task2.usTaskPrio = TASK_PRIO_TEST - 1;
    task2.uwResved = LOS_TASK_STATUS_DETACHED;

    ret = LOS_TaskCreate(&g_AnswerTaskID02,&task2);

    return LOS_OK;
}

static UINT32 Create_Task03(){
    UINT32 ret;
    TSK_INIT_PARAM_S task3 = { 0 };
    task3.pfnTaskEntry = (TSK_ENTRY_FUNC)Task03;
    task3.uwStackSize = TASK_STACK_SIZE_TEST*10;
    task3.pcName = "Task501-750";
    task3.usTaskPrio = TASK_PRIO_TEST - 1;
    task3.uwResved = LOS_TASK_STATUS_DETACHED;

    ret = LOS_TaskCreate(&g_AnswerTaskID03,&task3);

    return LOS_OK;
}

static UINT32 Create_Task04(){
    UINT32 ret;
    TSK_INIT_PARAM_S task4 = { 0 };
    task4.pfnTaskEntry = (TSK_ENTRY_FUNC)Task04;
    task4.uwStackSize = TASK_STACK_SIZE_TEST*10;
    task4.pcName = "Task751-1000";
    task4.usTaskPrio = TASK_PRIO_TEST - 1;
    task4.uwResved = LOS_TASK_STATUS_DETACHED;

    ret = LOS_TaskCreate(&g_AnswerTaskID04,&task4);

    return LOS_OK;
}

static UINT32 Create_HyperTask01(){
    UINT32 ret;
    TSK_INIT_PARAM_S task5 = { 0 };
    task5.pfnTaskEntry = (TSK_ENTRY_FUNC)HyperTask01;
    task5.uwStackSize = TASK_STACK_SIZE_TEST*10;
    task5.pcName = "HyperTask01";
    task5.usTaskPrio = TASK_PRIO_TEST - 4;
    task5.uwResved = LOS_TASK_STATUS_DETACHED;

    ret = LOS_TaskCreate(&g_AnswerTaskID05,&task5);

    return LOS_OK;
}

static UINT32 Create_HyperTask02(){
    UINT32 ret;
    TSK_INIT_PARAM_S task6 = { 0 };
    task6.pfnTaskEntry = (TSK_ENTRY_FUNC)HyperTask02;
    task6.uwStackSize = TASK_STACK_SIZE_TEST*10;
    task6.pcName = "HyperTask02";
    task6.usTaskPrio = TASK_PRIO_TEST-2;
    task6.uwResved = LOS_TASK_STATUS_DETACHED;

    ret = LOS_TaskCreate(&g_AnswerTaskID06,&task6);

    return LOS_OK;
}

static UINT32 Create_HyperTask03(){
    UINT32 ret;
    TSK_INIT_PARAM_S task7 = { 0 };
    task7.pfnTaskEntry = (TSK_ENTRY_FUNC)HyperTask03;
    task7.uwStackSize = TASK_STACK_SIZE_TEST*10;
    task7.pcName = "HyperTask03";
    task7.usTaskPrio = TASK_PRIO_TEST-2;
    task7.uwResved = LOS_TASK_STATUS_DETACHED;

    ret = LOS_TaskCreate(&g_AnswerTaskID07,&task7);

    return LOS_OK;
}

static UINT32 Create_HyperTask04(){
    UINT32 ret;
    TSK_INIT_PARAM_S task8 = { 0 };
    task8.pfnTaskEntry = (TSK_ENTRY_FUNC)HyperTask04;
    task8.uwStackSize = TASK_STACK_SIZE_TEST*10;
    task8.pcName = "HyperTask04";
    task8.usTaskPrio = TASK_PRIO_TEST-2 ;
    task8.uwResved = LOS_TASK_STATUS_DETACHED;

    ret = LOS_TaskCreate(&g_AnswerTaskID08,&task8);

    return LOS_OK;
}

static UINT32 Create_HyperTask05(){
    UINT32 ret;
    TSK_INIT_PARAM_S task9 = { 0 };
    task9.pfnTaskEntry = (TSK_ENTRY_FUNC)HyperTask05;
    task9.uwStackSize = TASK_STACK_SIZE_TEST*10;
    task9.pcName = "HyperTask05";
    task9.usTaskPrio = TASK_PRIO_TEST-2;
    task9.uwResved = LOS_TASK_STATUS_DETACHED;

    ret = LOS_TaskCreate(&g_AnswerTaskID09,&task9);

    return LOS_OK;
}

VOID Answer4_Demo(UINT32 argc,CHAR**argv){
    UINT32 ret;

    printf("\nAnswer4_demo launch in c\n");

    printf("\nargc is %d\n",argc);
    for(int i=0;i<argc;i++){
		printf("\nargv[%d] is: %s\n",i,argv[i]);	
	}
    
    //参数处理
    if(argc==1){
        printf("\nPlease input the param\n");
        return;
    }

    // if(argc>4){
    //     printf("\ntoo much param!\n");
    //     return;
    // }

    switch(argv[1][1]){
        case 'M':
        case 'm':{
            g_syncMode = 1;
            break;
        }
        case 'S':
        case 's':{
            g_syncMode = 2;
            break;
        }
        case 'E':
        case 'e':{
            g_syncMode = 3;
            break;
        }
        case 'Q':
        case 'q':{
            g_syncMode = 4;
            break;
        }
        case 'n':
        case 'N':{
            //高级模式
            g_syncMode = 5;
            break;
        }
        default:{
            printf("\nparam1 error!\n");
            return;
        }
    }

    //进入高级模式
    if(g_syncMode==5){
        printf("\nenter hyper mode!\n");

        digit = atoi(argv[2]);

        switch(argv[3][1]){
            case 'f':
            case 'F':{
                printf("\n----------------------------------------------------\n");
                break;
            }
            default:{
                printf("\nhyper Mode param error!\n");
                return;
            }
        }

        fileNum = atoi(argv[4]);

        //开线程
        if(fileNum>0&&fileNum<=digit){
            
            ret = Create_HyperTask01();
            if(ret==LOS_OK){
                printf("\nCreate HyperTask01 success\n");
            }
            ret = Create_HyperTask02();
            if(ret==LOS_OK){
                printf("\nCreate HyperTask02 success\n");
            }
            ret = Create_HyperTask03();
            if(ret==LOS_OK){
                printf("\nCreate HyperTask03 success\n");
            }
            ret = Create_HyperTask04();
            if(ret==LOS_OK){
                printf("\nCreate HyperTask04 success\n");
            }
            ret = Create_HyperTask05();
            if(ret==LOS_OK){
                printf("\nCreate HyperTask05 success\n");
            }

            LOS_TaskDelay(200);
            //运行时间未知，就不删线程了
            // LOS_TaskDelete(g_AnswerTaskID05);
            // LOS_TaskDelete(g_AnswerTaskID06);
            // LOS_TaskDelete(g_AnswerTaskID07);
            // LOS_TaskDelete(g_AnswerTaskID08);
            // LOS_TaskDelete(g_AnswerTaskID09);

            printf("\nHyperTask Delete!\n");
        }
        else{
            printf("\nhyper mode num error!\n");
            return;
        }
    }

    else{
        switch(argv[2][1]){
            case 'P':
            case 'p':{
                g_saveMode = 1;
                break;
            }
            case 'A':
            case 'a':{
                g_saveMode = 2;
                break;
            }
            default:{
                printf("\nparam2 error!\n");
                return;
            }
        }
        

        if(argc==4){
            g_saveMode = 3;
        }
        printf("\ng_syncMode is %d  g_saveMode is %d\n",g_syncMode,g_saveMode);
        //参数处理完成

        //启动线程 其中savaMode == 3的情况在这里拆分成1和2
        if(g_saveMode==3){
            for(int i = 0;i<2;i++){
                if(i==0){
                    g_saveMode=1;
                }
                if(i==1){
                    g_saveMode=2;
                }
                ret = Create_Task01();
                if(ret==LOS_OK){
                    printf("\nCreate Task1 success\n");
                }
                ret = Create_Task02();
                if(ret==LOS_OK){
                    printf("\nCreate Task2 success\n");
                }
                ret = Create_Task03();
                if(ret==LOS_OK){
                    printf("\nCreate Task3 success\n");
                }
                ret = Create_Task04();
                if(ret==LOS_OK){
                    printf("\nCreate Task4 success\n");
                }

                LOS_TaskDelay(50);

                LOS_TaskDelete(g_AnswerTaskID01);
                LOS_TaskDelete(g_AnswerTaskID02);
                LOS_TaskDelete(g_AnswerTaskID03);
                LOS_TaskDelete(g_AnswerTaskID04);
            }
        }
        else{
            ret = Create_Task01();
            if(ret==LOS_OK){
                printf("\nCreate Task1 success\n");
            }
            ret = Create_Task02();
            if(ret==LOS_OK){
                printf("\nCreate Task2 success\n");
            }
            ret = Create_Task03();
            if(ret==LOS_OK){
                printf("\nCreate Task3 success\n");
            }
            ret = Create_Task04();
            if(ret==LOS_OK){
                printf("\nCreate Task4 success\n");
            }

            LOS_TaskDelay(50);

            LOS_TaskDelete(g_AnswerTaskID01);
            LOS_TaskDelete(g_AnswerTaskID02);
            LOS_TaskDelete(g_AnswerTaskID03);
            LOS_TaskDelete(g_AnswerTaskID04); 
        }
    }

    return;
}